"""
    Created By Khun on 2020/4/27
"""

__author__ = 'Khun'

from flask import request, render_template

from app.models import Manage, Throw
from app.manage import manage


@manage.route('/')
def index():
    """ 广告投放链接对应的页面 """
    host = request.host
    host_url = 'http://' + host.split(':')[0] + '/'

    host_query = Manage.query.filter_by(web_url=host_url).first()
    try:
        wx = host_query.wechat  # 获取微信
        tem = host_query.manage.temp_path  # 获取模板地址
        cnzz = host_query.cnzz
        copyright_ = host_query.copyright_
        base_js = host_query.base_js
        transfer = host_query.transfer
        third_api = host_query.third_api
        throw_tid = host_query.throw_tid
        # 获取外部链接，例如二维码
        url_path_obj = Throw.query.filter_by(tid=throw_tid).first_or_404()
        tem_obj = {
            'wx': wx.split('|'),
            'tem': '/templates/' + tem,
            'cnzz': cnzz,
            'copyright_': copyright_,
            'base_js': base_js,
            'transfer': transfer + ';',
            'third_api': third_api,
            'path_url': url_path_obj.path_url
        }
        return render_template('{tem}/index.html'.format(tem=tem), tem_obj=tem_obj)
    except Exception as e:
        return '<h1 style="color:red; text-align: center;">请联系本站管理员添加网站！</h1>'


@manage.route('/<temp_path>/detail')
def detail(temp_path):
    """ 获取模板详情页 """
    try:
        temp = Throw.query.filter_by(temp_path=temp_path).first_or_404()
        tem_obj = {
            'wx': ['test1', 'test2'],
            'tem': '/templates/' + temp.temp_path,
            'cnzz': 0,
            'copyright_': 'xxx有限公司',
            'base_js': '<script type="text/javascript">var base_js</script>',
            'transfer': 'var transfer',
            'third_api': 'var third_api',
            'path_url': temp.path_url
        }
        return render_template('{temp_path}/index.html'.format(temp_path=temp.temp_path), tem_obj=tem_obj)
    except Exception as e:
        return '<h1 style="color:red; text-align: center;">模板尚未上传服务器，请联系本站管理员添加相应模板！</h1>'
